package com.hghivln.blog.controller;

import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.entity.Music;
import com.hghivln.blog.pojo.dto.MusicDto;
import com.hghivln.blog.pojo.vo.MusicVo;
import com.hghivln.blog.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 歌曲表 前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/music")
public class MusicController {

    @Autowired
    private MusicService musicService;

    /**
     * 分页查询
     */
    @PostMapping("getByPage")
    public Result<?> loadAllLog(@RequestBody MusicDto musicDto) {
        return musicService.loadAllMusic(musicDto);
    }

    /**
     * 查询所有启用中的音乐
     */
    @GetMapping("getAllEnable")
    public Result<?> getAllEnable() {
        List<Music> list = musicService.getAllEnable();
        return new Result<>(list);
    }

    @PostMapping("save")
    public Result<?> save(@RequestBody MusicDto musicDto) {
        try {
            musicService.saveMusic(musicDto);
            return new Result<>("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("保存失败");
        }
    }

    @PutMapping("update")
    public Result<?> update(@RequestBody MusicDto musicDto) {
        try {
            musicService.updateByMId(musicDto);
            return new Result<>("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("修改失败");
        }
    }

    @DeleteMapping("delete/{id}")
    public Result<?> deleteById(@PathVariable Long id) {
        try {
            musicService.deleteById(id);
            return new Result<>("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("删除失败");
        }
    }

    @GetMapping("get/{id}")
    public Result<?> get(@PathVariable Long id) {
        MusicVo musicVo = musicService.getInfo(id);
        return new Result<>(musicVo);
    }

    /**
     * 改变启用状态
     */
    @PutMapping("updateEnable")
    public Result<?> toggleEnable(@RequestBody MusicDto musicDto) {
        musicService.toggleEnable(musicDto);
        return Result.ok();
    }
}
